<template>
   <div>
      <TypeNav />
      <div class="main">
         <div class="search-filter">
            <div class="filter-center">
               <!--bread-->
               <div class="bread">
                  <ul class="fl sui-breadcrumb">
                     <li>
                        <a href="#">全部结果</a>
                     </li>
                  </ul>
                  <!-- 面包屑 -->
                  <ul class="fl sui-tag">
                     <!-- 分类面包屑 -->
                     <li class="with-x" v-show="searchParams.categoryName">{{ searchParams.categoryName }}<i
                           @click="removeCategoryName">×</i></li>
                     <!-- 关键字面包屑 -->
                     <li class="with-x" v-show="searchParams.keyword">{{ searchParams.keyword }}<i
                           @click="removeKeyword">×</i>
                     </li>

                  </ul>
               </div>


               <div class="cate">
                  <span>分类</span>
                  <ul>
                     <li v-for="(cate, index) in categoryList" :key="index"
                        :class="activeCategory == index + 1 ? 'activeCategory' : ''"
                        @click="categoryevent(cate.categoryName, index + 1)">
                        {{ cate.categoryName }}
                     </li>
                  </ul>
               </div>
            </div>

         </div>

         <div class="py-container">
            <!--details-->
            <div class="details clearfix">
               <div class="no-goods" v-if="goodsList.length == 0">
                  <img src="./images/nofound.png" alt="">
                  <h2>对应筛选条件下没有找到商品，换个筛选条件吧</h2>
               </div>
               <!-- 销售产品列表 -->
               <div class="goods-list">
                  <ul class="yui3-g">
                     <li v-for="(good, index) in goodsList" :key="good.goods_id">
                        <router-link :to="`/detail/${good.goods_id}`" target="_blank">
                           <div class="list-wrap">
                              <div class="p-img">
                                 <!-- 路由跳转时要带商品id，跳转后的路由要用商品id展示具体哪个商品数据 -->
                                 <img v-lazy="good.goods_img" />
                              </div>
                              <div class="name">
                                 <h2>{{ good.goods_name }}</h2>
                              </div>
                              <div class="attr">
                                 <h3>{{ good.goods_title }}</h3>
                              </div>
                              <div class="price">
                                 <p>{{ good.goods_price }}元</p>
                              </div>
                           </div>
                        </router-link>
                     </li>
                  </ul>
               </div>
               <!-- 使用分页器-->
               <Pagination :pageNo="searchParams.pageNo" :pageSize="searchParams.pageSize" :total="total" :continues="5"
                  @getPageNo="getPageNo" />
            </div>
         </div>
      </div>
   </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import Pagination from '@/components/Pagination/index.vue';
export default {
   name: 'Search',
   components: {
      Pagination
   },
   data() {
      return {
         activeCategory: 0,
         searchParams: {
            categoryId: "",        //分类的id
            categoryName: "",       //分类名字
            keyword: "",            //关键字
            pageNo: 1,              //分页器用，代表目前第几页
            pageSize: 6,            //每一页展示数据个数
         },
      };
   },
   beforeMount() {
      //将query和params拷贝入searchParams
      if (this.$route.params.keyword !== undefined) {
         Object.assign(this.searchParams, this.$route.query, this.$route.params)
      }
   },
   mounted(options) {
      this.getData();
      console.log(options);
   },
   methods: {
      //向服务器发请求获取search模块数据,封装为一个函数，需要时调用
      getData() {
         this.$store.dispatch('getSearchList', this.searchParams);
      },
      //删除分类面包屑
      removeCategoryName() {
         //优化：不用空字符串，
         //用undefined是因为值为undefined不会向服务器发送请求，减小服务器压力
         this.searchParams.categoryName = '';
         this.searchParams.categoryId = "";
         //地址栏也要改：进行路由跳转(自我跳转)
         if (this.$route.params) {//永远true
            this.$router.push({ name: "search", params: this.$route.params })
         }
         this.getData();

      },
      //删除关键字面包屑
      removeKeyword() {
         this.searchParams.keyword = '';
         //地址栏也要改 ：进行路由跳转(自我跳转)
         if (this.$route.query) {//永远true
            this.$router.push({ name: "search", query: this.$route.query })
         }
         this.getData();

         //利用全局事件总线清空搜索栏内容
         this.$bus.$emit('clear');
      },
      //点击分类按分类搜索商品
      categoryevent(categoryName, index) {
         this.activeCategory = index
         this.searchParams.categoryName = categoryName;
         this.getData()
      },
      //自定义事件,获取当前第几页
      getPageNo(pageNo) {
         this.searchParams.pageNo = pageNo;
         this.getData();
      },

   },
   computed: {
      ...mapGetters(['goodsList', 'categoryList']),
      //获取search
      ...mapState({
         total: state => state.search.searchList.total
      }),
   },

   watch: {
      //监听路由的信息是否发生变化，如果发生变化，再次发起请求
      $route(newValue, oldValue) {
         //不用空字符串，用undefined是因为值为undefined不会向服务器发送请求，减小服务器压力
         this.searchParams.categoryId = '';
         this.searchParams.keyword = '';
         Object.assign(this.searchParams, this.$route.query, this.$route.params)
         this.getData();
      }

   }
}
</script>

<style lang="less" scoped>
* {
   padding: 0;
   margin: 0;
}

.main {
   margin: 10px 0;
   background: #f5f5f5;

   .search-filter {
      background-color: #ffffff;

      .filter-center {
         margin: 0 auto;
         width: 1200px;

         .bread {
            margin-bottom: 5px;
            overflow: hidden;

            .sui-breadcrumb {
               padding: 3px 15px;
               margin: 0;
               font-weight: 400;
               border-radius: 3px;
               float: left;

               li {
                  display: inline-block;
                  line-height: 20px;
                  font-size: 15px;

                  a {
                     color: #666;
                     text-decoration: none;

                     &:hover {
                        color: #4cb9fc;
                     }
                  }
               }
            }

            .sui-tag {
               margin-top: -5px;
               list-style: none;
               font-size: 0;
               line-height: 0;
               padding: 5px 0 0;
               margin-bottom: 18px;
               float: left;

               .with-x {
                  font-size: 12px;
                  margin: 3px 5px 5px 0;
                  display: inline-block;
                  overflow: hidden;
                  color: #000;
                  background: #f7f7f7;
                  padding: 0 7px;
                  height: 20px;
                  line-height: 20px;
                  font-size: 15px;
                  border: 1px solid #dedede;
                  white-space: nowrap;
                  transition: color 400ms;
                  cursor: pointer;

                  i {
                     margin-left: 10px;
                     cursor: pointer;
                     font: 400 14px tahoma;
                     display: inline-block;
                     height: 100%;
                     vertical-align: middle;
                  }

                  &:hover {
                     color: #28a3ef;
                  }
               }
            }
         }

         .cate {
            height: 70px;
            width: 900px;
            // margin: 15px 5px;
            margin-top: 15px;
            // margin-bottom: -1px;
            // margin-bottom: 20px;
            padding: 5px;

            span {
               font-size: 25px;
               left: 10px;
               margin-left: 1em;
            }

            ul {
               display: flex;
               border: 1px solid #e4e7ed;
               border-radius: 5px;
               border-bottom: none;
               border-right: 0;

               li {
                  width: 100px;
                  height: 40px;
                  border-right: 1px solid #e4e7ed;
                  border-bottom: 1px solid #e4e7ed;
                  text-align: center;
                  cursor: pointer;
                  width: 100px;
                  height: 40px;
                  line-height: 40px;
                  font-size: 16px;

               }

               .activeCategory {
                  // border-bottom: none;
                  background-color: #f5f5f5;
               }
            }
         }

      }


   }

   .py-container {
      width: 1200px;
      margin: 0 auto;


      .details {
         margin-bottom: 5px;

         .sui-navbar {
            overflow: visible;
            margin-bottom: 0;

            .filter {
               min-height: 40px;
               padding-right: 20px;
               background: #fbfbfb;
               border: 1px solid #e2e2e2;
               padding-left: 0;
               border-radius: 0;
               box-shadow: 0 1px 4px rgba(0, 0, 0, 0.065);

               .sui-nav {
                  position: relative;
                  left: 0;
                  display: block;
                  float: left;
                  margin: 0 10px 0 0;

                  li {
                     float: left;
                     line-height: 18px;

                     a {
                        display: block;
                        cursor: pointer;
                        padding: 11px 15px;
                        color: #777;
                        text-decoration: none;
                     }

                     &.active {
                        a {
                           background: #e1251b;
                           color: #fff;
                        }
                     }
                  }
               }
            }
         }

         .no-goods {
            margin: 20px;
            height: 400px;

            h2 {
               font-size: 20px;
               text-align: center;
            }

            img {
               height: 200px;
               width: 200px;
               display: block;
               margin: 15px auto;
            }
         }

         .goods-list {
            margin: 20px 0;

            ul {
               display: flex;
               flex-wrap: wrap;

               li {
                  height: 100%;
                  width: 220px;
                  margin: 10px 10px;
                  line-height: 28px;

                  a {
                     text-decoration: none;

                     .list-wrap {
                        background: #ffffff;
                        text-align: center;

                        .p-img {
                           text-align: center;
                           width: 100%;
                           height: 220px;

                           img {
                              max-width: 100%;
                              margin: auto;
                              height: auto;
                              max-height: 100%;
                              vertical-align: middle;
                           }
                        }

                        .name {
                           h2 {
                              font-size: 14px;
                              font-weight: 400;
                              color: #333;
                           }
                        }

                        .attr {
                           padding-left: 15%;
                           text-align: center;
                           width: 70%;
                           overflow: hidden;
                           margin-bottom: 8px;
                           min-height: 20px;
                           cursor: pointer;
                           line-height: 20px;
                           display: -webkit-box;
                           -webkit-box-orient: vertical;
                           -webkit-line-clamp: 1;

                           h3 {
                              font-size: 12px;
                              font-weight: 400;
                              color: #b0b0b0;
                           }
                        }

                        .price {

                           padding-left: 15px;
                           font-size: 16px;
                           color: #ff6700;

                        }

                     }

                     .list-wrap:hover {
                        z-index: 2;
                        -webkit-box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
                        box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
                        -webkit-transform: translate3d(0, -2px, 0);
                        transform: translate3d(0, -2px, 0);
                     }
                  }

               }
            }
         }

         .page {
            width: 733px;
            height: 66px;
            overflow: hidden;
            float: right;

            .sui-pagination {
               margin: 18px 0;

               ul {
                  margin-left: 0;
                  margin-bottom: 0;
                  vertical-align: middle;
                  width: 490px;
                  float: left;

                  li {
                     line-height: 18px;
                     display: inline-block;

                     a {
                        position: relative;
                        float: left;
                        line-height: 18px;
                        text-decoration: none;
                        background-color: #fff;
                        border: 1px solid #e0e9ee;
                        margin-left: -1px;
                        font-size: 14px;
                        padding: 9px 18px;
                        color: #333;
                     }

                     &.active {
                        a {
                           background-color: #fff;
                           color: #e1251b;
                           border-color: #fff;
                           cursor: default;
                        }
                     }

                     &.prev {
                        a {
                           background-color: #fafafa;
                        }
                     }

                     &.disabled {
                        a {
                           color: #999;
                           cursor: default;
                        }
                     }

                     &.dotted {
                        span {
                           margin-left: -1px;
                           position: relative;
                           float: left;
                           line-height: 18px;
                           text-decoration: none;
                           background-color: #fff;
                           font-size: 14px;
                           border: 0;
                           padding: 9px 18px;
                           color: #333;
                        }
                     }

                     &.next {
                        a {
                           background-color: #fafafa;
                        }
                     }
                  }
               }

               div {
                  color: #333;
                  font-size: 14px;
                  float: right;
                  width: 241px;
               }
            }
         }
      }
   }
}
</style>